/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.BidCommonUtil;
import com.apache.bid.common.Constant;
import com.apache.bid.common.TeaUtil;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.common.cache.BidDataBaseRunable;
import com.apache.bid.common.vo.BidItemCacheVo;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidApplyHistory;
import com.apache.bid.entity.BidInfoBaojia;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidItemHistory;
import com.apache.bid.entity.BidItemPrice;
import com.apache.bid.entity.BidItemVip;
import com.apache.bid.entity.BidMaxContent;
import com.apache.bid.entity.BidPub;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidApplyHistoryManager;
import com.apache.bid.manager.BidApplyManager;
import com.apache.bid.manager.BidInfoBaojiaManager;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidItemPriceManager;
import com.apache.bid.manager.BidItemVipManager;
import com.apache.bid.manager.BidMaxContentManager;
import com.apache.bid.manager.BidPubManager;
import com.apache.bid.service.cacheplugins.helper.ReductionPriceHelper;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;
import com.apache.tools.DateUtils.FmtStr;
import com.apache.tools.StrUtil;

/**
 * description:  标的统一处理辅助类
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class UnityItemPluginHelper {

	private Logger log = Logger.getLogger(getClass());
	private static UnityItemPluginHelper instance;
	private BidItemManager bidItemManager;
	private BidItemPriceManager bidItemPriceManager;
	private BidPubManager bidPubManager;
	private BidItemVipManager bidItemVipManager;
	private BidInfoBaojiaManager bidInfoBaojiaManager;
	private BidApplyManager bidApplyManager;
	private BidApplyHistoryManager bidApplyHistoryManager;
	private BidMaxContentManager bidMaxContentManager;

	private UnityItemPluginHelper() {
	}

	public static UnityItemPluginHelper getInstance() {
		if (null == instance) {
			instance = new UnityItemPluginHelper();
		}
		return instance;
	}

	/**
	 * description: 发布标的    
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void startItem(BidItemCacheVo itemVo, boolean isEditStatus) {
		itemVo.setItemStatus(Constant.getInstance().getItemConfig("statusPubslish"));
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		if (cache.checkCacheObject(cache.item_prefix + itemVo.getItemId())) {
			cache.updateCacheObject(itemVo);
		} else {
			cache.createCacheObject(itemVo);
		}
		ParamsVo<BidItemPrice> priceVo = new ParamsVo<BidItemPrice>();
		BidItemPrice price = new BidItemPrice();
		price.setItemId(itemVo.getItemId());
		price.setOpenTime(itemVo.getOpenTimePre());
		price.setFirstValue(itemVo.getFirstValuePre());
		price.setFirstBidTime(itemVo.getFirstBidTimePre());
		price.setCloseTime(itemVo.getCloseTimePre());
		priceVo.setObj(price);
		bidItemPriceManager.saveInfo(priceVo);
		setCacheItemVip(itemVo.getItemId());
		BidCacheFactory.getInstance().getCacheManager("itemPrice").createCacheObject(price);
		if ("T".equals(itemVo.getvFlag()) || Constant.BIDMODE_HELAN.equals(itemVo.getBidMode())) {
			ReductionPriceHelper.getInstance().setParams(itemVo.getItemId(), itemVo.getItemId());
		}
		if (isEditStatus) {//修改标的数据库中对应状态为已发布
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			BidItem bidItem = new BidItem();
			bidItem.setItemId(itemVo.getItemId());
			bidItem.setItemStatus(Constant.getInstance().getItemConfig("statusPubslish"));
			bidItem.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			vo.setObj(bidItem);
			bidItemManager.editInfo(vo);
		}
	}

	/**
	 * description: 暂停标的    
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void pauseItem(BidItemCacheVo itemVo, boolean isEditStatus) {
		itemVo.setItemStatus(Constant.getInstance().getItemConfig("statusSuspend"));
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		if (cache.checkCacheObject(cache.item_prefix + itemVo.getItemId())) {
			cache.updateCacheObject(itemVo);
		} else {
			cache.createCacheObject(itemVo);
		}
		ParamsVo<BidItemPrice> priceVo = new ParamsVo<BidItemPrice>();
		BidItemPrice price = null;
		if (cache.checkCacheObject(cache.item_price_prefix + itemVo.getItemId())) {
			price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemVo.getItemId());
		} else {
			priceVo.setInfoId(itemVo.getItemId());
			price = (BidItemPrice) bidItemPriceManager.getInfoById(priceVo);
			priceVo.setInfoId("");
		}
		price.setPauseTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		priceVo.setObj(price);
		bidItemPriceManager.editInfo(priceVo);
		cache.createCacheObject(cache.item_price_prefix + itemVo.getItemId(), price);
		//setCacheItemVip(itemVo.getItemId());
		if (isEditStatus) {//修改标的数据库中对应状态为暂停中
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			BidItem bidItem = new BidItem();
			bidItem.setItemId(itemVo.getItemId());
			bidItem.setItemStatus(Constant.getInstance().getItemConfig("statusSuspend"));
			vo.setObj(bidItem);
			bidItemManager.editInfo(vo);
		}
	}

	/**
	 * description: 继续标的    
	 * 需要修改item和price中firstBidTime及closeTime
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void resetItem(BidItemCacheVo itemVo, boolean isEditStatus) {
		itemVo.setItemStatus(Constant.getInstance().getItemConfig("statusPubslish"));
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		if (cache.checkCacheObject(cache.item_prefix + itemVo.getItemId())) {
			cache.updateCacheObject(itemVo);
		} else {
			cache.createCacheObject(itemVo);
		}
		ParamsVo<BidItemPrice> priceVo = new ParamsVo<BidItemPrice>();
		BidItemPrice price = null;
		if (cache.checkCacheObject(cache.item_price_prefix + itemVo.getItemId())) {
			price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemVo.getItemId());
		} else {
			priceVo.setInfoId(itemVo.getItemId());
			price = (BidItemPrice) bidItemPriceManager.getInfoById(priceVo);
			priceVo.setInfoId("");
			if (null == price) {
				price = new BidItemPrice();
				price.setItemId(itemVo.getItemId());
				priceVo.setObj(price);
				bidItemPriceManager.saveInfo(priceVo);
			}
		}
		price.setFirstValue(itemVo.getFirstValuePre());
		price.setOpenTime(itemVo.getOpenTimePre());
		price.setFirstBidTime(itemVo.getFirstBidTimePre());
		price.setCloseTime(itemVo.getCloseTimePre());
		priceVo.setObj(price);
		bidItemPriceManager.editInfo(priceVo);
		cache.createCacheObject(cache.item_price_prefix + itemVo.getItemId(), price);
		//setCacheItemVip(itemVo.getItemId());
		if (isEditStatus) {//修改标的数据库中对应状态为已发布
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			BidItem bidItem = new BidItem();
			bidItem.setItemId(itemVo.getItemId());
			bidItem.setFirstBidTimePre(itemVo.getFirstBidTimePre());
			bidItem.setCloseTimePre(itemVo.getCloseTimePre());
			bidItem.setItemStatus(Constant.getInstance().getItemConfig("statusPubslish"));
			bidItem.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			vo.setObj(bidItem);
			bidItemManager.editInfo(vo);
		}
	}

	/**
	 * description: 恢复标的    
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void restoreItem(BidItemHistory history, BidItem item) {
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		BidItemCacheVo itemVo = new BidItemCacheVo();
		itemVo.addItemInfo(item);
		itemVo.setItemStatus(Constant.getInstance().getItemConfig("statusPubslish"));
		if ("0".equals(history.getSessionId())) {
			ParamsVo pubvo = new ParamsVo();
			BidPub pub = new BidPub();
			pub.setItemId(history.getItemId());
			pubvo.setObj(pub);
			pub = (BidPub) bidPubManager.getInfoById(pubvo);
			itemVo.setPub(pub);
		}
		cache.createCacheObject(itemVo);
		ParamsVo<BidItemPrice> priceVo = new ParamsVo<BidItemPrice>();
		BidItemPrice price = new BidItemPrice();
		price.setItemId(itemVo.getItemId());
		price.setOpenTime(itemVo.getOpenTimePre());
		price.setFirstValue(itemVo.getFirstValuePre());
		price.setFirstBidTime(itemVo.getFirstBidTimePre());
		price.setCloseTime(itemVo.getCloseTimePre());
		priceVo.setObj(price);
		bidItemPriceManager.saveInfo(priceVo);

		ParamsVo vo = new ParamsVo();
		BidCacheFactory.getInstance().getCacheManager("itemPrice").createCacheObject(price);
		setCacheItemVip(itemVo.getItemId());
		if (Constant.SETYPE_HD.equals(history.getSessionType())) {
			setItemApply(history.getItemId());
			//			BidApply apply = new BidApply();
			//			apply.setItemId(itemVo.getItemId());
			//			vo.setObj(apply);
			//			BidCacheManager appCache = BidCacheFactory.getInstance().getCacheManager("apply");
			//			List<BidApply> list = bidApplyManager.getList(vo);
			//			if (!ToolsUtil.isEmpty(list)) {//添加apply缓存
			//				for (BidApply ay : list) {
			//					String str = BidCacheManager.apply_prefix + ay.getUserName() + "_" + itemVo.getItemId();
			//					appCache.createCacheObject(str, ay);
			//				}
			//			}
		}
		if ("T".equals(itemVo.getvFlag()) || Constant.BIDMODE_HELAN.equals(itemVo.getBidMode())) {
			ReductionPriceHelper.getInstance().setParams(itemVo.getItemId(), itemVo.getItemId());
		}
		//修改标的数据库中对应状态为已发布
		item.setItemStatus(Constant.getInstance().getItemConfig("statusPubslish"));
		item.setModifyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		vo.setObj(item);
		bidItemManager.saveInfo(vo);
	}

	/**
	 * description: 结束标的    
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean stopItem(String itemId) {
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		BidItemCacheVo itemVo = (BidItemCacheVo) cache.getCacheCloneByKey(cache.item_prefix + itemId);
		BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemId);
		if (Constant.BIDMODE_HELAN.equals(itemVo.getBidMode())) {//对荷兰式竞价特殊处理
			try {
				String bidTime = DateUtils.Now.fmt_yyyyMMdd_HHmmssSSS();
				String pricStatus = BidCommonUtil.getInstance().getStatus(price, bidTime);
				if (Constant.getInstance().getItemConfig("ending").equals(pricStatus)
						|| Constant.getInstance().getItemConfig("statusStop").equals(pricStatus)) {
				} else {
					int bidTimes = Integer.parseInt(StrUtil.doNull(price.getBidTimes(), "0"));
					int itemNum = Integer.parseInt(StrUtil.doNull(itemVo.getItemNum(), "1"));
					BigDecimal fall_lowerst_price = new BigDecimal(StrUtil.doNull(itemVo.getFallLowerstPrice(), "0"));
					BigDecimal bidValue = new BigDecimal(StrUtil.doNull(price.getFirstValue(), "0"));
					if ((itemNum > bidTimes && bidValue.compareTo(fall_lowerst_price) > 0)
							|| bidValue.compareTo(fall_lowerst_price) <= 0) {
						return true;
					}
				}
			} catch (ParseException e) {
			}
		}
		ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
		BidItem item = new BidItem();
		item.setItemId(String.valueOf(itemId));
		item.setItemStatus(Constant.getInstance().getItemConfig("statusStop"));
		vo.setObj(item);
		boolean delMark = bidItemManager.editInfo(vo);
		if (delMark) {
			if (Constant.SETYPE_ZC.equals(itemVo.getSessionType())) {
				startNextItem(cache, itemVo, price);
			}
			if (Constant.BIDMODE_YCXASC.equals(itemVo.getBidMode())
					|| Constant.BIDMODE_YCXDESC.equals(itemVo.getBidMode())) {
				baojiaStopItem(itemId, itemVo.getBidMode());//处理报价记录
			} else if (Constant.BIDMODE_FEJJZX.equals(itemVo.getBidMode())
					|| Constant.BIDMODE_FEJJFX.equals(itemVo.getBidMode())) {
				feBaojiaStopItem(itemId, itemVo.getBidMode());//处理份额报价
			}
			itemVo.setItemStatus(Constant.getInstance().getItemConfig("statusStop"));
			cache.updateCacheObject(itemVo);
			if (Constant.SETYPE_HD.equals(itemVo.getSessionType())) {//清除同步锁
				Constant.getInstance().removeJingjiaMapByKey(itemVo.getItemId());
			} else {
				removeNumCache(cache, itemVo.getSessionId(), itemId);
			}
		}
		return delMark;
	}

	/**
	 * description:  开启串行中的下一个标的
	 */
	private void startNextItem(BidCacheManager cache, BidItem itemVo, BidItemPrice sourcePrice) {
		BidSession session = (BidSession) cache.getCacheObjectByKey(cache.session_prefix + itemVo.getSessionId());
		if (!Validator.isEmpty(session) && "T".equals(session.getSerialCtrl())) {//是否串行
			ParamsVo<BidItem> vo = new ParamsVo<BidItem>();
			vo.setObj(itemVo);
			vo.setKey("nextItemBySeId");
			BidItem item = (BidItem) bidItemManager.execute(vo);
			if (Validator.isEmpty(item)) {
				return;
			}
			//间歇时间（秒）
			long lmTime = Long.parseLong(session.getFirstTime()) * 60;//变成秒
			long restTime = Long.parseLong(session.getRestTime()) * 60;
			String openTime = BidCommonUtil.getInstance().getMergeTime(sourcePrice.getCloseTime(),
					String.valueOf(restTime));
			String firstBidTime = BidCommonUtil.getInstance().getMergeTime(openTime, String.valueOf(lmTime));
			String closeTime = getItemCloseTime(itemVo.getBidMode(), session.getLimitTime(), firstBidTime);
			BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(BidCacheManager.item_price_prefix
					+ item.getItemId());
			price.setOpenTime(openTime);
			price.setFirstBidTime(firstBidTime);
			price.setCloseTime(closeTime);
			boolean mark = cache.updateCacheObject(BidCacheManager.item_price_prefix + item.getItemId(), price);
			if (mark) {//以下修改数据库
				ParamsVo<BidItemPrice> priceVo = new ParamsVo<BidItemPrice>();
				priceVo.setObj(price);
				BidDataBaseRunable runable2 = new BidDataBaseRunable(priceVo, "bidItemPriceManager", "update");
				BidCacheFactory.getInstance().exeDatabase(runable2);
				item.setOpenTimePre(openTime);
				item.setFirstBidTimePre(firstBidTime);
				item.setCloseTimePre(closeTime);
				vo.setObj(item);
				BidDataBaseRunable runable = new BidDataBaseRunable(vo, "bidItemManager", "update");
				BidCacheFactory.getInstance().exeDatabase(runable);
				BidItemCacheVo cacheVo = (BidItemCacheVo) cache.getCacheCloneByKey(BidCacheManager.item_prefix
						+ item.getItemId());
				cacheVo.addItemInfo(item);
				cache.updateCacheObject(cacheVo);
			}
			vo = null;
		}
	}

	/**
	 * description:  计算结束时间
	 * @return   
	 */
	public String getItemCloseTime(String bidMode, String limitTime, String firstBidTime) {
		String closeTime = "";
		if (Constant.BIDMODE_JTZX.equals(bidMode) || Constant.BIDMODE_JTFX.equals(bidMode)) {
			closeTime = BidCommonUtil.getInstance().getMergeTime(firstBidTime, limitTime);
		} else if (Constant.BIDMODE_YCXASC.equals(bidMode) || Constant.BIDMODE_YCXDESC.equals(bidMode)) {
			closeTime = firstBidTime;
		} else if (Constant.BIDMODE_HELAN.equals(bidMode)) {
			long limittime = Long.valueOf(limitTime) * 1000 * 2;
			try {
				Date data = new Date(DateUtils.toLong(DateUtils.parse(firstBidTime, "yyyy-MM-dd HH:mm:ss")) + limittime);
				closeTime = DateUtils.format(data, "yyyy-MM-dd HH:mm:ss");
			} catch (ParseException e) {
				long nowTime = DateUtils.strToLong(firstBidTime, FmtStr.yyyyMMdd_HHmmss) + limittime;
				closeTime = DateUtils.longToStr(nowTime, FmtStr.yyyyMMdd_HHmmss);
			}
		} else if (Constant.BIDMODE_FEJJZX.equals(bidMode) || Constant.BIDMODE_FEJJFX.equals(bidMode)) {
			closeTime = BidCommonUtil.getInstance().getMergeTime(firstBidTime, limitTime);
		}
		return closeTime;
	}

	/**
	 * description:  验证活动或标的是否具有发布条件
	 * @param itemId
	 * @param sessionId
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String checkPublish(String itemId, String sessionId) {
		ParamsVo vo = new ParamsVo();
		BidPub pub = new BidPub();
		if (Validator.isNull(itemId)) {
			pub.setSessionId(sessionId);
		} else {
			pub.setItemId(itemId);
		}
		vo.setObj(pub);
		pub = (BidPub) bidPubManager.getInfoById(vo);
		if (Validator.isEmpty(pub))
			return "属性信息未填写暂不能发布，请填写属生信息";
		BidMaxContent content = new BidMaxContent();
		content.setObjId(Validator.getDefaultStr(itemId, sessionId));
		vo.setObj(content);
		List<BidMaxContent> list = bidMaxContentManager.getList(vo);
		if (Validator.isEmpty(list))
			return "交易须知与规则未填写暂不能发布，请填写交易须知与规则";
		String cType = "";
		for (BidMaxContent mc : list) {
			cType += mc.getContentType() + ",";
		}
		if (cType.indexOf("报名须知") == -1)
			return "报名须知未填写暂不能发布，请填写报名须知";
		if (cType.indexOf("交易规则") == -1)
			return "交易规则未填写暂不能发布，请填写交易规则";
		if (cType.indexOf("公告") == -1)
			return "交易公告未填写暂不能发布，请填写交易公告";
		return "";
	}

	/**
	 * description:  清除允许购买数量的缓存
	 */
	private void removeNumCache(BidCacheManager cache, String sessionId, String itemId) {
		BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemId);
		String keyPrefix = BidCacheManager.bailCtrlNum_prefix + sessionId + "_" + price.getUserName();
		String keyPrefix2 = BidCacheManager.bailCtrlPrice_prefix + sessionId + "_" + price.getUserName();
		if (cache.checkCacheObject(keyPrefix)) {//处理按数量的缓存
			String numStr = String.valueOf(cache.getCacheObjectByKey(keyPrefix));
			if ("0".equals(numStr)) {
				BidCacheFactory.getInstance().getCacheManager("itemPrice").removeCacheObject(keyPrefix);
			} else
				cache.createCacheObject(keyPrefix, String.valueOf(Integer.parseInt(numStr) - 1));
		} else if (cache.checkCacheObject(keyPrefix2)) {//处理按保证金比例的缓存
			BigDecimal numStr = new BigDecimal(
					String.valueOf(cache.getCacheObjectByKey(keyPrefix + price.getUserName())));
			if (numStr.compareTo(new BigDecimal(0)) <= 0) {
				BidCacheFactory.getInstance().getCacheManager("itemPrice").removeCacheObject(keyPrefix2);
			} else {
				BigDecimal firstValue = new BigDecimal(price.getFirstValue());
				BidSession session = (BidSession) cache.getCacheObjectByKey(cache.session_prefix + sessionId);
				BigDecimal bail_rate = new BigDecimal(session.getBailRate());
				cache.createCacheObject(keyPrefix + price.getUserName(),
						String.valueOf(numStr.subtract(firstValue.multiply(bail_rate))));
			}
		}
	}

	/**
	 * description:  处理份额报价
	 * @param itemId   
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void feBaojiaStopItem(String itemId, String bidMode) {
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
		List<String> keys = (List) cache.getCacheObjectByKey(BidCacheManager.baojia_prefix + itemId);
		if (Validator.isEmpty(keys))
			return;
		List<BidInfoBaojia> list = cache.getObjectsByKeys(keys);
		ParamsVo vo = new ParamsVo();
		for (BidInfoBaojia baojia : list) {
			String lastValue = TeaUtil.decryptByTea(baojia.getBidValue());
			baojia.setBidValue(lastValue);
			vo.setObj(baojia);
			bidInfoBaojiaManager.editInfo(vo);
		}
		BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemId);
		price.setBidTimes(String.valueOf(list.size()));
		cache.updateCacheObject(price);
		vo.setObj(price);
		bidItemPriceManager.editInfo(vo);
		vo = null;
	}

	/**
	 * description:  处理一次报价
	 * @param itemId   
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void baojiaStopItem(String itemId, String bidMode) {
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("itemPrice");
		List<String> keys = (List) cache.getCacheObjectByKey(cache.baojia_prefix + itemId);
		if (Validator.isEmpty(keys))
			return;
		List<BidInfoBaojia> list = cache.getObjectsByKeys(keys);
		BidItemPrice price = (BidItemPrice) cache.getCacheCloneByKey(cache.item_price_prefix + itemId);
		boolean mark = Constant.BIDMODE_YCXASC.equals(bidMode) ? true : false;
		ParamsVo vo = new ParamsVo();
		for (BidInfoBaojia baojia : list) {
			String lastValue = TeaUtil.decryptByTea(baojia.getBidValue());
			BigDecimal myPrice = new BigDecimal(lastValue);
			BigDecimal bidValue = new BigDecimal(Validator.getDefaultStr(price.getLastValue(), "0"));
			//价格优先，时间优先原则
			if (mark) {
				if (myPrice.compareTo(bidValue) > 0) {//一次正向
					price.setLastValue(lastValue);
					price.setUserId(baojia.getUserId());
					price.setUserName(baojia.getUserName());
					price.setBidNum(baojia.getBidNum());
					price.setLastBidTime(baojia.getBidTime());
					price.setAmount(baojia.getAmount());
					price.setLastRightLevel(baojia.getRightLevel());
				}
			} else {
				if (myPrice.compareTo(bidValue) < 0 || bidValue.compareTo(new BigDecimal(0)) == 0) {//一次反向
					price.setLastValue(lastValue);
					price.setUserId(baojia.getUserId());
					price.setUserName(baojia.getUserName());
					price.setBidNum(baojia.getBidNum());
					price.setLastBidTime(baojia.getBidTime());
					price.setAmount(baojia.getAmount());
					price.setLastRightLevel(baojia.getRightLevel());
				}
			}
			if (myPrice == bidValue) {
				long nowTime = BidCommonUtil.getMillTime(price.getLastBidTime(), "yyyy-MM-dd HH:mm:ss");
				long bidTime = BidCommonUtil.getMillTime(baojia.getBidTime(), "yyyy-MM-dd HH:mm:ss");
				if (bidTime < nowTime) {
					price.setLastValue(lastValue);
					price.setUserId(baojia.getUserId());
					price.setUserName(baojia.getUserName());
					price.setBidNum(baojia.getBidNum());
					price.setLastBidTime(baojia.getBidTime());
					price.setAmount(baojia.getAmount());
					price.setLastRightLevel(baojia.getRightLevel());
				}
			}
			baojia.setBidValue(lastValue);
			vo.setObj(baojia);
			bidInfoBaojiaManager.editInfo(vo);
		}
		price.setBidTimes(String.valueOf(list.size()));
		cache.updateCacheObject(price);
		vo.setObj(price);
		bidItemPriceManager.editInfo(vo);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void setItemApply(String itemId) {
		ParamsVo vo = new ParamsVo();
		BidApplyHistory apply = new BidApplyHistory();
		apply.setItemId(itemId);
		vo.setObj(apply);
		List<BidApplyHistory> list = bidApplyHistoryManager.getList(vo);
		if (!Validator.isEmpty(list)) {
			BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("apply");
			ParamsVo bavo = new ParamsVo();
			vo.setObj(null);
			for (BidApplyHistory bah : list) {
				BidApply ba = new BidApply();
				BeanUtils.copyProperties(bah, ba);
				bavo.setObj(ba);
				String id = bidApplyManager.saveInfo(bavo);
				if (Validator.isNotNull(id)) {
					cache.createCacheObject(ba);
					vo.setInfoId(bah.getApplyId());
					bidApplyHistoryManager.deleteInfo(vo);
				}
			}
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void setCacheItemVip(String itemId) {
		ParamsVo<BidItemVip> vo = new ParamsVo<BidItemVip>();
		BidItemVip vip = new BidItemVip();
		vip.setVipStatus("1");//正常状态
		vip.setItemId(itemId);
		vo.setObj(vip);
		List<BidItemVip> list = bidItemVipManager.getList(vo);
		if (!Validator.isEmpty(list)) {
			BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("itemVip");
			for (BidItemVip itemVip : list) {
				String key = BidCacheManager.item_vip_prefix + itemVip.getUserEname() + "_" + itemId;
				if (cache.checkCacheObject(key)) {
					cache.updateCacheObject(itemVip);
				} else {
					cache.createCacheObject(itemVip);
				}
			}
		}
	}

	public void init() {
		instance = this;
	}

	public void destroy() {
		instance = null;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

	public void setBidItemPriceManager(BidItemPriceManager bidItemPriceManager) {
		this.bidItemPriceManager = bidItemPriceManager;
	}

	public void setBidPubManager(BidPubManager bidPubManager) {
		this.bidPubManager = bidPubManager;
	}

	public void setBidItemVipManager(BidItemVipManager bidItemVipManager) {
		this.bidItemVipManager = bidItemVipManager;
	}

	public void setBidInfoBaojiaManager(BidInfoBaojiaManager bidInfoBaojiaManager) {
		this.bidInfoBaojiaManager = bidInfoBaojiaManager;
	}

	public void setBidApplyManager(BidApplyManager bidApplyManager) {
		this.bidApplyManager = bidApplyManager;
	}

	public void setBidApplyHistoryManager(BidApplyHistoryManager bidApplyHistoryManager) {
		this.bidApplyHistoryManager = bidApplyHistoryManager;
	}

	public void setBidMaxContentManager(BidMaxContentManager bidMaxContentManager) {
		this.bidMaxContentManager = bidMaxContentManager;
	}
}
